package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesSaleOrderChangeRpcService;
import com.hunttown.mes.rpc.domain.MesSaleOrderChangeDTO;
import com.hunttown.mes.rpc.domain.query.MesSaleOrderChangeDTOQuery;
import com.hunttown.mes.service.MesSaleOrderChangeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesSaleOrderChangeConverts.convert;

/**
 * created by wangjunfu on 2022-09-08 15:57:50
 */
@Service
public class MesSaleOrderChangeRpcServiceImpl implements MesSaleOrderChangeRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesSaleOrderChangeRpcServiceImpl.class);

    private final MesSaleOrderChangeService mesSaleOrderChangeService;

    @Autowired
    public MesSaleOrderChangeRpcServiceImpl(MesSaleOrderChangeService mesSaleOrderChangeService) {
        this.mesSaleOrderChangeService = mesSaleOrderChangeService;
    }

    @Override
    public MesSaleOrderChangeDTO insert(MesSaleOrderChangeDTO mesSaleOrderChangeDTO) {
        try {
            return convert(mesSaleOrderChangeService.insert(convert(mesSaleOrderChangeDTO)));
        } catch (Exception e) {
            logger.error("MesSaleOrderChange create failed.", e);
            throw new RuntimeException("MesSaleOrderChange create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesSaleOrderChangeDTO mesSaleOrderChangeDTO) {
        try {
            return mesSaleOrderChangeService.updateInfoById(convert(mesSaleOrderChangeDTO));
        } catch (Exception e) {
            logger.error("MesSaleOrderChange update by id failed.", e);
            throw new RuntimeException("MesSaleOrderChange update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesSaleOrderChangeDTOQuery mesSaleOrderChangeDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesSaleOrderChangeDTOQuery.getW_id() != null && mesSaleOrderChangeDTOQuery.getW_id() > 0) {
                query.put("w_id", mesSaleOrderChangeDTOQuery.getW_id());
            }

            if (mesSaleOrderChangeDTOQuery.getIds() != null && !mesSaleOrderChangeDTOQuery.getIds().equals("")) {
                query.put("ids", mesSaleOrderChangeDTOQuery.getIds());
            }

            if (mesSaleOrderChangeDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesSaleOrderChangeDTOQuery.getStopFlag());
            }

            if (mesSaleOrderChangeDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesSaleOrderChangeDTOQuery.getDeleteFlag());
            }

            return mesSaleOrderChangeService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesSaleOrderChange update by query failed.", e);
            throw new RuntimeException("MesSaleOrderChange update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesSaleOrderChangeDTO selectById(Integer id) {
        try {
            return convert(mesSaleOrderChangeService.selectById(id));
        } catch (Exception e) {
            logger.error("MesSaleOrderChange select by id failed.", e);
            throw new RuntimeException("MesSaleOrderChange select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesSaleOrderChangeDTO selectObjByQuery(MesSaleOrderChangeDTOQuery mesSaleOrderChangeDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesSaleOrderChangeDTOQuery.getOrderId() != null) {
                query.put("orderId", mesSaleOrderChangeDTOQuery.getOrderId());
            }

            if (mesSaleOrderChangeDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesSaleOrderChangeDTOQuery.getStopFlag());
            }

            if (mesSaleOrderChangeDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesSaleOrderChangeDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesSaleOrderChangeDTOQuery.getOrderBy())) {
                query.put("orderBy", mesSaleOrderChangeDTOQuery.getOrderBy());
            }

            return convert(mesSaleOrderChangeService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesSaleOrderChange select obj by query failed.", e);
            throw new RuntimeException("MesSaleOrderChange select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesSaleOrderChangeDTO> getListForPage(MesSaleOrderChangeDTOQuery mesSaleOrderChangeDTOQuery) {
        Query query = Query.page(mesSaleOrderChangeDTOQuery.getPn(), mesSaleOrderChangeDTOQuery.getPageSize());

        if (mesSaleOrderChangeDTOQuery.getId() != null && mesSaleOrderChangeDTOQuery.getId() > 0) {
            query.put("id", mesSaleOrderChangeDTOQuery.getId());
        }

        if (mesSaleOrderChangeDTOQuery.getIds() != null && !mesSaleOrderChangeDTOQuery.getIds().equals("")) {
            query.put("ids", mesSaleOrderChangeDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesSaleOrderChangeDTOQuery.getLikeName())) {
            query.put("likeName", mesSaleOrderChangeDTOQuery.getLikeName());
        }

        if (mesSaleOrderChangeDTOQuery.getOrderId() != null) {
            query.put("orderId", mesSaleOrderChangeDTOQuery.getOrderId());
        }

        if (StringUtils.isNotBlank(mesSaleOrderChangeDTOQuery.getShowDate())) {
            query.put("showDate", mesSaleOrderChangeDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesSaleOrderChangeDTOQuery.getStartDate())) {
            query.put("startDate", mesSaleOrderChangeDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesSaleOrderChangeDTOQuery.getEndDate())) {
            query.put("endDate", mesSaleOrderChangeDTOQuery.getEndDate());
        }

        if (mesSaleOrderChangeDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", mesSaleOrderChangeDTOQuery.getStopFlag());
        }

        if (mesSaleOrderChangeDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesSaleOrderChangeDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesSaleOrderChangeDTOQuery.getOrderBy())) {
            query.put("orderBy", mesSaleOrderChangeDTOQuery.getOrderBy());
        }

        List<MesSaleOrderChangeDTO> list = convert(mesSaleOrderChangeService.selectByQuery(query));
        Page<MesSaleOrderChangeDTO> page = Page.from(mesSaleOrderChangeDTOQuery.getPn(), mesSaleOrderChangeDTOQuery.getPageSize(), list);
        page.setTotalItems(mesSaleOrderChangeService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesSaleOrderChangeService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesSaleOrderChange delete failed.", e);
            throw new RuntimeException("MesSaleOrderChange delete failed, Please order your code.", e);
        }
    }
}